package com.climbingGuide.java;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListaSectores extends ListActivity{
	public String[] listaDatos;
	//public int[] tipo;
	private BaseDatos db2;
	public ActionBar actionBar;
	
	public class ViewWrapper{
		View base;
		TextView label;
		ImageView icon;
		ImageView arrow;
		
		public ViewWrapper(View base){
			this.base = base;
		}
		
		public TextView getLabel(){
			if (label == null)
				label = (TextView)base.findViewById(R.id.texto);
			return label;
		}
		
		public ImageView getIcon(){
			if (icon == null)
				icon = (ImageView)base.findViewById(R.id.icono);
			return icon;
		}
		public ImageView getArrow(){
			if (arrow == null)
				arrow = (ImageView)base.findViewById(R.id.arrow);
			return arrow;
		}
	} 
	public class MiAdaptador extends ArrayAdapter<String>{
		Context contexto;

		public MiAdaptador(Context context) {
			super(context, R.layout.fila_list, listaDatos);
			contexto = context;
		}		
		@Override
		public View getView (int position, View convertView, ViewGroup parent){
			//Aprovechamos convertView para no crear nueva fila
			View fila = convertView;
			ViewWrapper envoltorio = null;
			
			if (fila == null){
				fila = View.inflate(this.contexto, R.layout.fila_list, null);
				envoltorio = new ViewWrapper(fila);
				fila.setTag(envoltorio);
			}else
				envoltorio = (ViewWrapper)fila.getTag();
			
			TextView label = envoltorio.getLabel();			
			label.setText(listaDatos[position]);
			
			
			ImageView iconito = envoltorio.getIcon();
			iconito.setImageResource(R.drawable.rock_climbing24);
			ImageView flecha = envoltorio.getArrow();
			flecha.setImageResource(android.R.drawable.ic_media_play);
			return fila;
		}	
	}
	
    public static Intent createIntent(Context context) {
        Intent i = new Intent(context, ClimbingGuideActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return i;
    }
	public class CloseAction extends AbstractAction {

		public CloseAction(int arg0) {
			super(arg0);
		}
        @Override
        public void performAction(View view) {
    
            //Toast.makeText(ClimbingGuideActivity.this,"texto", Toast.LENGTH_SHORT).show();
        	finish();
        }
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.own_listview);
        
        actionBar = (ActionBar)findViewById(R.id.actionbar2);
        //actionBar.setHomeLogo(android.R.drawable.ic_dialog_map);
        //actionBar.addAction(new ExampleAction(android.R.drawable.ic_dialog_info));
        //actionBar.setHomeAction(new IntentAction(this, createIntent(this), android.R.drawable.ic_dialog_map));
        actionBar.setHomeAction(new CloseAction(android.R.drawable.ic_dialog_map));
        actionBar.setDisplayHomeAsUpEnabled(true);

        db2 = new BaseDatos(this);
        try {        	 
     		db2.abrirBD();   
     	}catch(SQLException sqle){     
     		throw sqle;
     	}
     	Cursor c = db2.getInfoMap(false, new String[]{"Nombre", "Tipo"}, null, null, null, null, "nombre ASC");
     	listaDatos = new String[c.getCount()];
        //tipo = new int[c.getCount()];
        int i = 0;
        while (c.moveToNext()){
        	listaDatos[i] = c.getString(0);
        	//tipo[i] = c.getInt(1);
        	i++;
        }
        c.close();
        setListAdapter(new MiAdaptador(ListaSectores.this));
        actionBar.setTitle(listaDatos.length + " sectores");

	}
	@Override
	protected void onListItemClick(ListView l,View v, int position, long id){
		
		Intent intent = new Intent(ListaSectores.this, InfoSector.class);
		intent.putExtra("sectorName", listaDatos[position]);
		startActivity(intent);
	}
	
	@Override
	protected void onDestroy(){
		db2.close();
    	super.onDestroy();
    		//bdTab2.cerrarBd();
    }
}

